package com.micangcaihang.micang.adapter;

import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.micangcaihang.common.utils.ConvertUtils;
import com.micangcaihang.common.utils.TimeUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.entity.TaskListEntity;

import java.util.List;

/**
 * 作者：Fandy on 2017/7/23 19:52
 * 邮箱：fandy618@hotmail.com
 */

public class DTabBackTaskAdapter extends BaseMultiItemQuickAdapter<TaskListEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public DTabBackTaskAdapter(List<TaskListEntity> data) {
        super(data);
        addItemType(TaskListEntity.TASK_NEW, R.layout.item_new_task_d);
        addItemType(TaskListEntity.TASK_TO_BE_TREATED, R.layout.item_to_be_treated_d);
        addItemType(TaskListEntity.TASK_DATA_ERROR, R.layout.item_data_error_d);
        addItemType(TaskListEntity.TASK_DATA_INCOMPLETE, R.layout.item_data_incomplete_d);
    }

    @Override
    protected void convert(BaseViewHolder helper, TaskListEntity item) {
        switch (helper.getItemViewType()) {
            case TaskListEntity.TASK_NEW:
                helper.setText(R.id.tv_address, item.getLocated_address());
                try {
                    helper.setText(R.id.tv_add_time, TimeUtils.millis2StringMinute(Long.parseLong(item.getApply_time()) * 1000));
                } catch (Exception e) {
                    helper.setText(R.id.tv_add_time, "");
                }
                helper.setText(R.id.tv_customer_name, String.format(mContext.getString(R.string.format_customer_name), item.getCustomer_name()));
                helper.setText(R.id.tv_loan_type_name, String.format(mContext.getString(R.string.format_loan_type_name), item.getLoan_type_name()));
                helper.setText(R.id.tv_saler_name, String.format(mContext.getString(R.string.format_saler_name), item.getSaler_name()));
                break;
            case TaskListEntity.TASK_TO_BE_TREATED:
                helper.setText(R.id.tv_address, item.getLocated_address());
                try {
                    helper.setText(R.id.tv_add_time, TimeUtils.millis2StringMinute(Long.parseLong(item.getApply_time()) * 1000));
                } catch (Exception e) {
                    helper.setText(R.id.tv_add_time, "");
                }
                helper.setText(R.id.tv_customer_name, String.format(mContext.getString(R.string.format_customer_name), item.getCustomer_name()));
                helper.setText(R.id.tv_loan_type_name, String.format(mContext.getString(R.string.format_loan_type_name), item.getLoan_type_name()));
                helper.setText(R.id.tv_saler_name, String.format(mContext.getString(R.string.format_saler_name), item.getSaler_name()));
                break;
            case TaskListEntity.TASK_DATA_ERROR:
                helper.setText(R.id.tv_address, item.getLocated_address());
                try {
                    helper.setText(R.id.tv_add_time, TimeUtils.millis2StringMinute(Long.parseLong(item.getApply_time()) * 1000));
                } catch (Exception e) {
                    helper.setText(R.id.tv_add_time, "");
                }
                helper.setText(R.id.tv_customer_name, String.format(mContext.getString(R.string.format_customer_name), item.getCustomer_name()));
                helper.setText(R.id.tv_loan_type_name, String.format(mContext.getString(R.string.format_loan_type_name), item.getLoan_type_name()));
                helper.setText(R.id.tv_saler_name, String.format(mContext.getString(R.string.format_saler_name), item.getSaler_name()));
                try {
                    helper.setText(R.id.tv_director_time, String.format(mContext.getString(R.string.format_director_time), TimeUtils.millis2StringMinute(Long.parseLong(item.getReback_time()) * 1000)));
                } catch (Exception e) {
                    helper.setText(R.id.tv_director_time, "");
                }
                break;
            case TaskListEntity.TASK_DATA_INCOMPLETE:
                helper.setText(R.id.tv_address, item.getLocated_address());
                try {
                    helper.setText(R.id.tv_add_time, TimeUtils.millis2StringMinute(Long.parseLong(item.getApply_time()) * 1000));
                } catch (Exception e) {
                    helper.setText(R.id.tv_add_time, "");
                }
                helper.setText(R.id.tv_customer_name, String.format(mContext.getString(R.string.format_customer_name), item.getCustomer_name()));
                helper.setText(R.id.tv_loan_type_name, String.format(mContext.getString(R.string.format_loan_type_name), item.getLoan_type_name()));
                helper.setText(R.id.tv_saler_name, String.format(mContext.getString(R.string.format_saler_name), item.getSaler_name()));
                try {
                    helper.setText(R.id.tv_director_time, String.format(mContext.getString(R.string.format_director_time), TimeUtils.millis2StringMinute(Long.parseLong(item.getReback_time()) * 1000)));
                } catch (Exception e) {
                    helper.setText(R.id.tv_director_time, "");
                }
                helper.setText(R.id.tv_reback_department, String.format(mContext.getString(R.string.format_reback_department), item.getReback_department()));
                break;
            default:
                break;
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp_11 = ConvertUtils.dp2px(11);
        int dp_22 = ConvertUtils.dp2px(22);
        if (helper.getLayoutPosition() == 0) {
            params.setMargins(dp_11, dp_22, dp_11, dp_11);
        } else if (helper.getLayoutPosition() == getItemCount() - 1) {
            params.setMargins(dp_11, dp_11, dp_11, dp_22);
        } else {
            params.setMargins(dp_11, dp_11, dp_11, dp_11);
        }
        helper.itemView.setLayoutParams(params);
    }
}
